//
//  TKYLoginController.m
//  StationHouseHelper
//
//  Created by apple on 2018/5/4.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYLoginController.h"
#import "TKYUserModel.h"
#import "TKYTabBarController.h"
@interface TKYLoginController ()
@property (weak, nonatomic) IBOutlet UIImageView *logoImageV;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoImageConstraintY;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoImageConstraintH;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *logoImageContraintW;

@end

@implementation TKYLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    // Do any additional setup after loading the view from its nib.
}
/**
 设置界面
 */
- (void)setUpUI{
    if (iPhone5SE) {
        self.logoImageConstraintY.constant = 50;
        self.logoImageConstraintH.constant = 70;
        self.logoImageContraintW.constant = 70;

    }
   
    [self.logoImageV tky_cornerWithRadius:10 borderColor:nil borderWidth:0];
    [self.loginButton tky_cornerWithRadius:5 borderColor:nil borderWidth:0];
    self.userNameTF.kx_placeholderColor = [UIColor whiteColor];
    self.passwordTF.kx_placeholderColor = [UIColor whiteColor];
    self.passwordTF.secureTextEntry = YES;
    self.passwordTF.clearButtonMode = UITextFieldViewModeAlways;
    self.userNameTF.clearButtonMode = UITextFieldViewModeAlways;
    self.userNameTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:KUSERNAMEKEY];
    self.passwordTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:KPWDKEY];
}

#pragma mark - 用户名为nil / 密码为nil
- (BOOL)isProcessLogin {
    if (self.userNameTF.text.length == 0 ) {
        KALERTSHOW(@"提示", @"账号不能为空", @"确定");
        return NO;
    }
    if (self.passwordTF.text.length == 0) {
        KALERTSHOW(@"提示", @"密码不能为空", @"确定");
    }
    return YES;
}

- (IBAction)loginButtonClick:(UIButton *)sender {
    // 0.判断是否允许登录
    NSString *url = [NSString stringWithFormat:@"%@%@",kApiBaseUrl,kLoginUrl];
    NSDictionary *parametDict = @{
                                  @"username":self.userNameTF.text,
                                  @"password":self.passwordTF.text
                                  };
    TKYTabBarController *tabbarVC = [[TKYTabBarController alloc]init];
    [self presentViewController:tabbarVC animated:YES completion:nil];
    if (![self isProcessLogin]) return;
    [SVProgressHUD show];
    [KHttpRequestTool POST:url parameters:parametDict success:^(id response) {
        NSLog(@"%@",response);
        if ([response[@"code"] integerValue ] == 0) {
            TKYUserModel *userModel = [TKYUserModel mj_objectWithKeyValues:response[@"userInfo"][@"user"] ];
            //保存用户的token
            [[NSUserDefaults  standardUserDefaults]setObject:response[@"userInfo"][@"token"] forKey:kToken];
            [[NSUserDefaults standardUserDefaults]setObject:@"expire" forKey:kExpire];
            [[NSUserDefaults standardUserDefaults]setObject:userModel.userId forKey:kUserID];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
            [TKYUserModel sharedUserModel].userId = userModel.userId;
            [TKYUserModel sharedUserModel].account = userModel.account;
            [TKYUserModel sharedUserModel].email = userModel.email;
            [TKYUserModel sharedUserModel].username = userModel.username;
            //保存用户名密码
            [self saveAccountAndPassword];
            TKYTabBarController *tabbarVC = [[TKYTabBarController alloc]init];
            [self presentViewController:tabbarVC animated:YES completion:nil];
            [SVProgressHUD dismiss];

        }else{
            [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:response[@"msg"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [SVProgressHUD showSVProgressImage:nil Duration:2.0 FontSize:16 status:@"网络错误"];
    }];
}

/**
 *保存用户名密码
 */
- (void)saveAccountAndPassword{
    NSString *userName = self.userNameTF.text;
    NSString *pwd = self.passwordTF.text;
    [[NSUserDefaults standardUserDefaults] setObject:userName forKey:KUSERNAMEKEY];
    [[NSUserDefaults standardUserDefaults] setObject:pwd forKey:KPWDKEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
